/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bbd.crawler.url;

import bbd.crawler.url.fwk.BaseURLHandler;

import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author Amogh
 */
public class SimpleURLReplaceHandler extends BaseURLHandler {

    private ArrayList tokenList = null;

    private ArrayList mXn2DList = null;

    private int index      = -1;
    private int valueCount = -1;

    //--------------------------------------------------------------------------
    /** Javadoc here */
    public SimpleURLReplaceHandler () { } // end default constructor

    //--------------------------------------------------------------------------
    /** Javadoc here */
    public SimpleURLReplaceHandler ( String url, ArrayList list2DNxN ) {
        init ( url, list2DNxN );
    } // end constructor

    //--------------------------------------------------------------------------
    /** Javadoc here */
    public String getUrl() {
        
        String url = null;

        if ( hasNext() ) {

            url = getOriginalURL();

            for ( int i = 0; i < tokenList.size(); i++ ) {

                String token = (String) tokenList.get(i);

                String value = getFormattedValue (
                                    (String) ( (ArrayList) mXn2DList.get(i) ).get(index)
                                );

                token = token.replaceAll ( "\\{", "\\\\{" ).replaceAll ( "\\}", "\\\\}" );

                url = url.replaceAll ( token, value );
            }

            index ++;
        }

        return url;

    } // end method getUrl

    //--------------------------------------------------------------------------
    /** Javadoc here */
    public boolean hasNext() {

        return ( index > -1
                   && index < valueCount );

    } // end method hasNext

    //--------------------------------------------------------------------------
    /** Javadoc here */
    public void init ( String url, ArrayList list2DMxN ) {
        
        boolean throwException = false;

        if ( list2DMxN.size() > 0 ) {

            if ( ( (ArrayList)list2DMxN.get(0) ).size() > 0 ) {

                valueCount = ( (ArrayList)list2DMxN.get(0) ).size();

                for ( int i = 0; i < list2DMxN.size(); i++ ) {

                    if ( ( (ArrayList)list2DMxN.get(i) ).size() != valueCount ) {
                        
                        throwException = true;
                    }
                }
            }
            else { throwException = true; }
        }
        else { throwException = true; }

        if ( throwException ) {

            throw new RuntimeException ( 
                        "{SimpleURLReplaceHandler.init} expecting M x N 2D ArrayList for url " + url
                   );
        }
        else {

            setOriginalURL (url);
            
            mXn2DList = list2DMxN;

            tokenList = parseForTokens();

            if ( tokenList.size() != mXn2DList.size() ) {

                String message = "{SimpleURLReplaceHandler.init} token count [" + tokenList.size() + "] " +
                                  "doesn't match value list size [" + mXn2DList.size() + "] " +
                                  "for url " + url;

                throw new RuntimeException (message);
            }

            index = 0;
        }
        
    } // end method init

    //--------------------------------------------------------------------------
    /** Javadoc here */
    public String getcurrentValue() {

        String csv = new String ("");

        Iterator iterator = mXn2DList.iterator();

        while ( iterator.hasNext() ) {

            csv = csv.concat ( 
                        (String) ((ArrayList) iterator.next()).get (index)
                      );

            if ( iterator.hasNext() ) {
                csv = csv.concat ( ", " );
            }
        }

        return csv;

    } // end method getcurrentValue

    //--------------------------------------------------------------------------
    /** Javadoc here */
    public ArrayList getcurrentValues() {
        
        ArrayList<String> values = new ArrayList();

        Iterator iterator = mXn2DList.iterator();

        while ( iterator.hasNext() ) {

            values.add (
                    (String) ((ArrayList) iterator.next()).get (index)
                   );
        }
        
        return values;

    } // end method getcurrentValues

} // end class SimpleURLReplaceHandler
